import jdk.swing.interop.SwingInterOpUtils;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class Control {

    // 合并两个方快
    // @param
    public void mergeBlock(Block bl1, Block bl2) {
        if (bl1.isMerge(bl2)) {
            bl1.doubleNum();
            bl2.setNum(0);
        } else {
            throw new RuntimeException("方块合并错误");
        }
    }


    // 移动交换位置
    public void switchBlock(Block bl1, Block bl2) {
        bl1.setNum(bl2.getNum());
        bl1.setNum(0);
    }

    // 上移+合并+上移
    public boolean upMove(Board board) {
        // 上移
        for (int i = 1; i < Config.row; i++) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = 0; k < i; k++) {
                        if (board.board.get(k).get(j).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(pos).get(j).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        // 合并
        for (int i = 0; i < Config.row - 1; i++) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).isMerge(board.board.get(i + 1).get(j))) {
                    mergeBlock(board.board.get(i).get(j), board.board.get(i + 1).get(j));
                }
            }
        }
        // 上移
        for (int i = 1; i < Config.row; i++) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = 0; k < i; k++) {
                        if (board.board.get(k).get(j).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(pos).get(j).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        return true;
    }

    // 下移+合并+下移
    public boolean downMove(Board board) {
        // 下移
        for (int i = Config.row - 2; i >= 0; i--) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = Config.row - 1; k > i; k--) {
                        if (board.board.get(k).get(j).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(pos).get(j).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        // 合并
        for (int i = Config.row - 1; i >= 1; i--) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).isMerge(board.board.get(i - 1).get(j))) {
                    mergeBlock(board.board.get(i).get(j), board.board.get(i - 1).get(j));
                }
            }
        }
        // 下移
        for (int i = Config.row - 2; i >= 0; i--) {
            for (int j = 0; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = Config.row - 1; k > i; k--) {
                        if (board.board.get(k).get(j).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(pos).get(j).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        return true;
    }

    //  左移+合并+左移
    public boolean leftMove(Board board) {
        // 左移
        for (int i = 0; i < Config.row; i++) {
            for (int j = 1; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找第i行的第j列之前的0方块,然后交换
                    int pos = -1;
                    for (int k = 0; k < j; k++) {
                        if (board.board.get(i).get(k).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(i).get(pos).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        // 合并
        for (int i = 0; i < Config.row; i++) {
            for (int j = 0; j < Config.column - 1; j++) {
                if (board.board.get(i).get(j).isMerge(board.board.get(i).get(j + 1))) {
                    mergeBlock(board.board.get(i).get(j), board.board.get(i).get(j + 1));
                }
            }
        }
        // 左移
        for (int i = 0; i < Config.row; i++) {
            for (int j = 1; j < Config.column; j++) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找第i行的第j列之前的0方块,然后交换
                    int pos = -1;
                    for (int k = 0; k < j; k++) {
                        if (board.board.get(i).get(k).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(i).get(pos).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        return true;
    }

    // 右移+合并+右移
    public boolean rightMove(Board board) {
        // 右移
        for (int i = 0; i < Config.row; i++) {
            for (int j = Config.column - 2; j >= 0; j--) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = Config.column - 1; k > j; k--) {
                        if (board.board.get(i).get(k).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(i).get(pos).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        // 合并
        for (int i = 0; i < Config.row; i++) {
            for (int j = Config.column - 1; j >= 1; j--) {
                if (board.board.get(i).get(j).isMerge(board.board.get(i).get(j - 1))) {
                    mergeBlock(board.board.get(i).get(j), board.board.get(i).get(j - 1));
                }
            }
        }
        // 右移
        for (int i = 0; i < Config.row; i++) {
            for (int j = Config.column - 2; j >= 0; j--) {
                if (board.board.get(i).get(j).getNum() != 0) {
                    // 找i之前的0方块,然后交换
                    int pos = -1;
                    for (int k = Config.column - 1; k > j; k--) {
                        if (board.board.get(i).get(k).getNum() == 0) {
                            pos = k;
                            break;
                        }
                    }
                    if (pos != -1) {
                        // 第j列：第pos行和第i行交换
                        board.board.get(i).get(pos).setNum(board.board.get(i).get(j).getNum());
                        board.board.get(i).get(j).setNum(0);
                    }
                }
            }
        }
        return true;
    }
}
